import cv2
--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) Input In [1], in <cell line: 1>() ----> 1 import cv2 ModuleNotFoundError: No module named 'cv2'
install cv2
Input In [2] install cv2 ^ SyntaxError: invalid syntax
import cv2
--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) Input In [3], in <cell line: 1>() ----> 1 import cv2 ModuleNotFoundError: No module named 'cv2'
import cv2
from matplotlib import pyplot as plt
image_file="C:\\Users\\sharp25\\Downloads\\Tanuja Kothapalli _ DL Copy.jpg"
img=cv2.imread(image_file)
def display(im_path):
dpi = 80
im_data = plt.imread(im_path)
height, width = im_data.shape[:2]
# What size does the figure need to be in inches to fit the image?
figsize = width / float(dpi), height / float(dpi)
# Create a figure of the right size with one axes that takes up the full figure
fig = plt.figure(figsize=figsize)
ax = fig.add_axes([0, 0, 1, 1])
# Hide spines, ticks, etc.
ax.axis('off')
# Display the image.
ax.imshow(im_data, cmap='hsv')
plt.show()
display(image_file)
inverted_image = cv2.bitwise_not(img)
cv2.imwrite("C:/Users/sharp25/Downloads/inverted.jpg", inverted_image)
True
display("C:/Users/sharp25/Downloads/inverted.jpg")
def grayscale(image):
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray_image = grayscale(img)
cv2.imwrite("C:/Users/sharp25/Downloads/gray.jpg", gray_image)
True
display("C:/Users/sharp25/Downloads/gray.jpg")
thresh, im_bw = cv2.threshold(gray_image, 100, 300, cv2.THRESH_BINARY)
cv2.imwrite("C:/Users/sharp25/Downloads/bw_image.jpg", im_bw)
True
display("C:/Users/sharp25/Downloads/bw_image.jpg")
def noise_removal(image):
import numpy as np
kernel = np.ones((1, 1), np.uint8)
image = cv2.dilate(image, kernel, iterations=1)
kernel = np.ones((1, 1), np.uint8)
image = cv2.erode(image, kernel, iterations=1)
image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)
image = cv2.medianBlur(image, 3)
return (image)
no_noise = noise_removal(im_bw)
cv2.imwrite("C:/Users/sharp25/Downloads/no_noise.jpg", no_noise)
True
display("C:/Users/sharp25/Downloads/no_noise.jpg")
def thin_font(image):
import numpy as np
image = cv2.bitwise_not(image)
kernel = np.ones((2,2),np.uint8)
image = cv2.erode(image, kernel, iterations=1)
image = cv2.bitwise_not(image)
return (image)
eroded_image = thin_font(no_noise)
cv2.imwrite("C:/Users/sharp25/Downloads/eroded_image.jpg", eroded_image)
True
display("C:/Users/sharp25/Downloads/eroded_image.jpg")
pip install pytesseract
Collecting pytesseract Downloading pytesseract-0.3.9-py2.py3-none-any.whl (14 kB) Requirement already satisfied: Pillow>=8.0.0 in c:\users\sharp25\anaconda3\lib\site-packages (from pytesseract) (9.0.1) Requirement already satisfied: packaging>=21.3 in c:\users\sharp25\anaconda3\lib\site-packages (from pytesseract) (21.3) Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in c:\users\sharp25\anaconda3\lib\site-packages (from packaging>=21.3->pytesseract) (3.0.4) Installing collected packages: pytesseract Successfully installed pytesseract-0.3.9 Note: you may need to restart the kernel to use updated packages.
import pytesseract
from PIL import Image
img_file="C:/Users/sharp25/OneDrive/Desktop/ocr/PragnaGundala_DL.jpg"
img=Image.open(img_file)
ocr_result =pytesseract.image_to_string(img)
print(ocr_result)
FEDERAL LIMITS APPLY 4d eo 1GUNDAI SPRAGNA 3pos 02/09/1989 —_—aaiss 12/07/2019 8 14660 NE 31ST ST APT C306 a BELLEVUE WA 98007-7641 45 SEX F 18 EYES BLK 16 HGT 5'-01" 47WGT 164 & wexe 02/0 5DD WDL7TST77' '207193A1354 . REV 11/12/2019
import cv2
from matplotlib import pyplot as plt
image_file = "C:/Users/sharp25/OneDrive/Desktop/ocr/img.jpg"
img = cv2.imread(image_file)
def display(im_path):
dpi = 80
im_data = plt.imread(im_path)
height, width = im_data.shape[:2]
# What size does the figure need to be in inches to fit the image?
figsize = width / float(dpi), height / float(dpi)
# Create a figure of the right size with one axes that takes up the full figure
fig = plt.figure(figsize=figsize)
ax = fig.add_axes([0, 0, 1, 1])
# Hide spines, ticks, etc.
ax.axis('off')
# Display the image.
ax.imshow(im_data, cmap='gray')
plt.show()
display(image_file)
inverted_image = cv2.bitwise_not(img)
cv2.imwrite('C:/Users/sharp25/OneDrive/Desktop/ocr/inverted.jpg',inverted_image)
True
display("C:/Users/sharp25/OneDrive/Desktop/ocr/inverted.jpg")
def grayscale(image):
return cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
gray_image = grayscale(img)
cv2.imwrite("C:/Users/sharp25/OneDrive/Desktop/ocr/gray.jpg", gray_image)
True
display("C:/Users/sharp25/OneDrive/Desktop/ocr/gray.jpg")
thresh, im_bw = cv2.threshold(gray_image, 210, 230, cv2.THRESH_BINARY)
cv2.imwrite("C:/Users/sharp25/OneDrive/Desktop/ocr/bw_image.jpg", im_bw)
True
display("C:/Users/sharp25/OneDrive/Desktop/ocr/bw_image.jpg")
def noise_removal(image):
import numpy as np
kernel = np.ones((1, 1), np.uint8)
image = cv2.dilate(image, kernel, iterations=1)
kernel = np.ones((1, 1), np.uint8)
image = cv2.erode(image, kernel, iterations=1)
image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)
image = cv2.medianBlur(image, 3)
return (image)
no_noise = noise_removal(im_bw)
cv2.imwrite("C:/Users/sharp25/OneDrive/Desktop/ocr/no_noise.jpg", no_noise)
True
display("C:/Users/sharp25/OneDrive/Desktop/ocr/no_noise.jpg")
def thin_font(image):
import numpy as np
image = cv2.bitwise_not(image)
kernel = np.ones((2,2),np.uint8)
image = cv2.erode(image, kernel, iterations=1)
image = cv2.bitwise_not(image)
return (image)
eroded_image = thin_font(no_noise)
cv2.imwrite("C:/Users/sharp25/OneDrive/Desktop/ocr/eroded_image.jpg", eroded_image)
True
display("C:/Users/sharp25/OneDrive/Desktop/ocr/eroded_image.jpg")
def thick_font(image):
import numpy as np
image = cv2.bitwise_not(image)
kernel = np.ones((1,1),np.uint8)
image = cv2.dilate(image, kernel, iterations=1)
image = cv2.bitwise_not(image)
return (image)
dilated_image = thick_font(no_noise)
cv2.imwrite("C:/Users/sharp25/OneDrive/Desktop/ocr/dilated_image.jpg", dilated_image)
True
display("C:/Users/sharp25/OneDrive/Desktop/ocr/dilated_image.jpg")
new = cv2.imread("C:/Users/sharp25/OneDrive/Desktop/ocr/hqdefault.jpg")
display("C:/Users/sharp25/OneDrive/Desktop/ocr/hqdefault.jpg")
#https://becominghuman.ai/how-to-automatically-deskew-straighten-a-text-image-using-opencv-a0c30aed83df
import numpy as np
def getSkewAngle(cvImage) -> float:
# Prep image, copy, convert to gray scale, blur, and threshold
newImage = cvImage.copy()
gray = cv2.cvtColor(newImage, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (9, 9), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Apply dilate to merge text into meaningful lines/paragraphs.
# Use larger kernel on X axis to merge characters into single line, cancelling out any spaces.
# But use smaller kernel on Y axis to separate between different blocks of text
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (30, 5))
dilate = cv2.dilate(thresh, kernel, iterations=1)
# Find all contours
contours, hierarchy = cv2.findContours(dilate, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key = cv2.contourArea, reverse = True)
for c in contours:
rect = cv2.boundingRect(c)
x,y,w,h = rect
cv2.rectangle(newImage,(x,y),(x+w,y+h),(0,255,0),2)
# Find largest contour and surround in min area box
largestContour = contours[0]
print (len(contours))
minAreaRect = cv2.minAreaRect(largestContour)
cv2.imwrite("temp/boxes.jpg", newImage)
# Determine the angle. Convert it to the value that was originally used to obtain skewed image
angle = minAreaRect[-1]
if angle < -45:
angle = 90 + angle
return -1.0 * angle
# Rotate the image around its center
def rotateImage(cvImage, angle: float):
newImage = cvImage.copy()
(h, w) = newImage.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
newImage = cv2.warpAffine(newImage, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
return newImage
# Deskew image
def deskew(cvImage):
angle = getSkewAngle(cvImage)
return rotateImage(cvImage, -1.0 * angle)
fixed = deskew(new)
cv2.imwrite("C:/Users/sharp25/OneDrive/Desktop/ocr/rotated_fixed.jpg", fixed)
9
True
display("C:/Users/sharp25/OneDrive/Desktop/ocr/rotated_fixed.jpg")
display("C:/Users/sharp25/OneDrive/Desktop/ocr/rotated_fixed.jpg")
def remove_borders(image):
contours, heiarchy = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cntsSorted = sorted(contours, key=lambda x:cv2.contourArea(x))
cnt = cntsSorted[-1]
x, y, w, h = cv2.boundingRect(cnt)
crop = image[y:y+h, x:x+w]
return (crop)
no_borders = remove_borders(rotated_fixed)
cv2.imwrite("C:/Users/sharp25/OneDrive/Desktop/ocr/no_borders.jpg", no_borders)
display('C:/Users/sharp25/OneDrive/Desktop/ocr/no_borders.jpg')
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Input In [104], in <cell line: 1>() ----> 1 no_borders = remove_borders(rotated_fixed) 2 cv2.imwrite("C:/Users/sharp25/OneDrive/Desktop/ocr/no_borders.jpg", no_borders) 3 display('C:/Users/sharp25/OneDrive/Desktop/ocr/no_borders.jpg') NameError: name 'rotated_fixed' is not defined